library(readr)
library(readxl)
library(openxlsx)
library(igraph)

## Statistics and dispersion // ���������� � �������������

summary(net_f)
summary(net_c)

d.net_f <- degree(net_f)
d.net_c <- degree(net_c)

par(mfrow=c(1,2))
hist(d.net_f, col="lightblue", xlab="Vertex Degree", ylab="Frequency", main='Regions')
hist(d.net_c, col="pink", xlab="Vertex Degree", ylab="Frequency", main='Cities')

## Federated Subjects of the Russian Federation // �������� �� 

nodes_f <- read_csv("nod-fed-states-stats.csv")
edges_f <- read_csv("ed-fed-states.csv")
net_f <- graph_from_data_frame(d=edges_f, vertices=nodes_f, directed=F)
net_f <- simplify(net_f, remove.loops = T)
coul <- c("cyan1","brown1","darkgoldenrod1")
my_color <- coul[as.numeric(as.factor(V(net_f)$level))]
V(net_f)$label.color="black"
V(net_f)$vertex.label=nodes_f$Id
V(net_f)$color= my_color
V(net_f)$size=2+(15 * nodes_f$betweenesscentrality)
V(net_f)$shape="circle"
V(net_f)$shape = ifelse(V(net_f)$status == 'disputed', "csquare", V(net_f)$shape)
net_f_lkf <- layout.kamada.kawai(net_f)
net_f_fr <- layout.fruchterman.reingold(net_f)

set.seed(1204)
plot(net_f, layout=net_f_fr, vertex.label=NA)

set.seed(1204)
plot(net_f, layout=net_f_fr)


## Cities of the Russian Federation // ������ �� 

nodes_c <- read_csv("nod-cities-stats.csv")
edges_c <- read_csv("ed-cities.csv")
net_c <- graph_from_data_frame(d=edges_c, vertices=nodes_c, directed=F)
net_c <- simplify(net_c, remove.loops = T)
V(net_c)$label.color="black"
V(net_c)$vertex.label=nodes_c$Id
coul1 <- c("cyan1","darkgoldenrod1")
my_color1 <- coul1[as.numeric(as.factor(V(net_c)$att_rus))]
V(net_c)$color= my_color1
V(net_c)$size=2+(15 * nodes_c$betweenesscentrality)
V(net_c)$shape="circle"
V(net_c)$shape = ifelse(V(net_c)$shape.act == 1, "sphere", V(net_c)$shape)
V(net_c)$shape = ifelse(V(net_c)$shape.act == 3, "square", V(net_c)$shape)
net_c_fr <- layout.fruchterman.reingold(net_c)
net_c_lkf <- layout.kamada.kawai(net_c)

set.seed(1204)
plot(net_c, layout=net_c_fr, vertex.label=NA)

set.seed(1204)
plot(net_c, layout=net_c_fr)

par(mfrow=c(1,2))
set.seed(1204)
plot(net_f, layout=net_f_fr, vertex.label=NA, main='Network of Regions')
plot(net_c, layout=net_c_fr, vertex.label=NA, main='Network of Cities')

